package com.onetech.adrewards.free.fragments;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.onetech.adrewards.free.R;
import com.onetech.adrewards.free.fragments.v2.HomeFragment;

public class ExchangeFragmentNew extends BaseFragment {
	private View mExchangeBtn;
	private View mExchangeHistoryBtn;
	private View mPointHistoryBtn;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (mView == null) {
			mView = inflater.inflate(R.layout.fragment_exchange_new, container,
					false);
			initView();
			initListener();
			menuSelected(1);
		} else {
			((ViewGroup) mView.getParent()).removeView(mView);
		}
		return mView;
	}

	private void initListener() {
		mExchangeBtn.setOnClickListener(this);
		mExchangeHistoryBtn.setOnClickListener(this);
		mPointHistoryBtn.setOnClickListener(this);
	}

	private void initView() {
		mExchangeBtn = (LinearLayout) mView.findViewById(R.id.exchangeFragment);
		mExchangeHistoryBtn = (LinearLayout) mView
				.findViewById(R.id.exchangeHistory);
		mPointHistoryBtn = (LinearLayout) mView.findViewById(R.id.pointHistory);
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.exchangeFragment:
			menuSelected(1);
			break;
		case R.id.exchangeHistory:
			menuSelected(2);
			break;
		case R.id.pointHistory:
			menuSelected(3);
			break;
		default:
			break;
		}
		super.onClick(v);
	}

	public void switchCurrentItem(int index) {
		((HomeFragment) getParentFragment()).switchCurrentItem(index);
	}

	public void loadTabHistoryExchanged() {

		initView();
		initListener();
		mExchangeBtn.setBackgroundResource(R.drawable.button_blue_exchage);
		mExchangeHistoryBtn
				.setBackgroundResource(R.drawable.button_yellow_exchage);
		mPointHistoryBtn.setBackgroundResource(R.drawable.button_blue_exchage);
		Fragment fragment = null;
		FragmentManager fragmentManager = getActivity()
				.getSupportFragmentManager();
		fragment = ExchangeFragment.instantiate(getActivity()
				.getApplicationContext(), ExchangeFragment.class.getName());
		if (fragment != null) {
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();
		}
	}

	public void menuSelected(int index) {
		Fragment fragment = null;
		FragmentManager fragmentManager =  getChildFragmentManager();
		
		switch (index) {
		case 1:
			mExchangeBtn
					.setBackgroundResource(R.drawable.button_yellow_exchage);
			mExchangeHistoryBtn
					.setBackgroundResource(R.drawable.button_blue_exchage);
			mPointHistoryBtn
					.setBackgroundResource(R.drawable.button_blue_exchage);
			fragment = ListGiftCardFragment.instantiate(getActivity()
					.getApplicationContext(), ListGiftCardFragment.class
					.getName());
			break;
		case 2:
			mExchangeBtn.setBackgroundResource(R.drawable.button_blue_exchage);
			mExchangeHistoryBtn
					.setBackgroundResource(R.drawable.button_yellow_exchage);
			mPointHistoryBtn
					.setBackgroundResource(R.drawable.button_blue_exchage);
			fragment = ExchangeFragment.instantiate(getActivity()
					.getApplicationContext(), ExchangeFragment.class.getName());
			break;
		case 3:
			mExchangeBtn.setBackgroundResource(R.drawable.button_blue_exchage);
			mExchangeHistoryBtn
					.setBackgroundResource(R.drawable.button_blue_exchage);
			mPointHistoryBtn
					.setBackgroundResource(R.drawable.button_yellow_exchage);
			fragment = PointHistoryFragment.instantiate(getActivity()
					.getApplicationContext(), PointHistoryFragment.class
					.getName());
			break;

		default:
			break;
		}
		if (fragment != null) {
			fragmentManager.beginTransaction()
					.replace(R.id.content_frame, fragment).commit();
		}
	}
}
